package com.farvision.common.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author niufw
 * @Date 2021/12/2 15:24
 * @Version 1.0
 */

public class WorkDayCalculateUtils {

    /**
     * 特殊的工作日(星期六、日工作)
     */
    private static final List<String> SPECIAL_WORK_DAYS = new ArrayList<>();

    /**
     * 特殊的休息日(星期一到五休息)
     */
    private static final List<String> SPECIAL_REST_DAYS = new ArrayList<>();

    /**
     * @param currentDate
     * @param num
     * @param dateUnit
     * @param workHours   工作日历中的每天工作时长
     * @return
     */
    public static Date getDate(Date currentDate, Float num, DateUnit dateUnit, Float workHours) {
        if (num == 0) {
            return currentDate;
        }

        //初始化为开始时间？
        Date endDate = currentDate;
        //单位是小时
        if (dateUnit.equals(DateUnit.HOUR)) {
            Integer numDays = Convert.toInt(Math.ceil(num/workHours));
            endDate = getEndDate(currentDate, numDays);
        } else if (dateUnit.equals(DateUnit.DAY)) {
            endDate = getEndDate(currentDate, Convert.toInt(num));
        }
        return endDate;
    }

    /**
     * 手工维护特殊日(因为是未知的,所以必须手工维护)
     */
    public static void initSpecialDays(List<String> specialWorkDays, List<String> specialRestDays) {
//        SPECIAL_WORK_DAYS.add("2019-09-29");
//        SPECIAL_WORK_DAYS.add("2019-10-12");
//
//        SPECIAL_REST_DAYS.add("2019-10-01");
//        SPECIAL_REST_DAYS.add("2019-10-02");
//        SPECIAL_REST_DAYS.add("2019-10-03");
//        SPECIAL_REST_DAYS.add("2019-10-04");
//        SPECIAL_REST_DAYS.add("2019-10-07");

        SPECIAL_WORK_DAYS.clear();
        SPECIAL_WORK_DAYS.addAll(specialWorkDays);
        SPECIAL_REST_DAYS.clear();
        SPECIAL_REST_DAYS.addAll(specialRestDays);
    }

    private static Date getEndDate(Date currentDate, Integer days) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);

        int step = days < 0 ? -1 : 1;
        int i = 0;
        int numAbs = Math.abs(days);
        while (i < numAbs) {
            calendar.add(Calendar.DATE, step);
            i++;
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                    || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {

                // 周六日如果上班就算1个工作日
                if (!SPECIAL_WORK_DAYS.contains(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd"))) {
                    i--;
                }
            } else {
                // 周1到周五休息就算1个休息日
                if (SPECIAL_REST_DAYS.contains(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd"))) {
                    i--;
                }
            }
        }
        return calendar.getTime();
    }

    /**
     * 获取两个时间段内的工作日天数,不包含enddate
     * 如 2021-11-01 到 2021-12-01
     * @param startDate
     * @param endDate
     * @return
     */
    public  static  Float getWorkdays(Date startDate,Date endDate)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        Long i = 0L;
        Date tmp = calendar.getTime();
        while(!tmp.equals(endDate))
        {

            calendar.add(Calendar.DATE, 1);
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                    || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {

                // 周六日如果上班就算1个工作日
                if (SPECIAL_WORK_DAYS.contains(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd"))) {
                    i++;
                }
            } else {
                // 周1到周五休息就算1个休息日
                if (SPECIAL_REST_DAYS.contains(DateFormatUtils.format(calendar.getTime(), "yyyy-MM-dd"))) {
                    i--;
                }
                else
                {
                    i++;
                }
            }
            tmp = calendar.getTime();
        }
        return  Convert.toFloat(i);
    }
}
